﻿Public Partial Class Pre_Product_popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData()
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            ViewState("index") = ""
            If Not Session("PORT_PRODUCT") Is Nothing Then
                ViewState("Port") = Session("PORT_PRODUCT")
                AutoCompleteExtender3.ContextKey = ViewState("Port")


                If Not (Session("IndexProduct") Is Nothing) Then
                    Dim dt As DataTable = Session("Product_popup")
                    Dim index As Integer = CInt(Session("IndexProduct"))
                    ViewState("index") = index
                    If Not (ddlProduct.Items.FindByValue(dt.Rows(index).Item("Product_Event")) Is Nothing) Then
                        ddlProduct.SelectedValue = dt.Rows(index).Item("Product_Event")
                    End If
                    txtProductName.Text = dt.Rows(index).Item("Product_name")
                    txtProduct.Text = dt.Rows(index).Item("Product_Amount")
                    txtDescription.Text = dt.Rows(index).Item("Product_Description")
                    If Not (ddlUnit.Items.FindByValue(dt.Rows(index).Item("Product_Unit")) Is Nothing) Then
                        ddlUnit.SelectedValue = dt.Rows(index).Item("Product_Unit")
                    End If

                End If
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("PORT_PRODUCT") = Nothing
        Session("IndexProduct") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('D');", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Session("PORT_PRODUCT") = Nothing
        Session("IndexProduct") = Nothing
        If addProduct() Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal('P');", True)
        End If


    End Sub

    Private Function addProduct() As Boolean
        If ((txtProduct.Text.Trim() = "") Or (txtProductName.Text.Trim() = "")) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุข้อมูลสินค้า !!');", True)
            Return False
        End If
        Dim product As DataTable = clsGet.Get_Product_no(txtProductName.Text)
        Dim ds As DataTable = Session("Product_popup")

        Dim Product_no As String = ""
        Dim Group_no As String = ""
        Dim Group_name As String = ""
        If (product.Rows.Count = 0) Then
            Product_no = "0000000000"
            Group_no = "0000000000"
            Group_name = "-"
        Else
            Product_no = product.Rows(0).Item("Product_no")
            Group_no = product.Rows(0).Item("Group_no")
            Group_name = product.Rows(0).Item("Group_name")
        End If
        If (ds.Rows(0).Item("Product_name") & "" = "") Then
            ds.Rows(0).Item("Product_no") = Product_no
            ds.Rows(0).Item("Product_name") = txtProductName.Text
            ds.Rows(0).Item("Product_Amount") = txtProduct.Text
            ds.Rows(0).Item("Product_Event") = ddlProduct.SelectedValue
            ds.Rows(0).Item("Product_Description") = txtDescription.Text
            ds.Rows(0).Item("Product_Unit") = ddlUnit.SelectedValue
            ds.Rows(0).Item("Group_no") = Group_no
            ds.Rows(0).Item("Group_name") = Group_name
            If (ViewState("Port") = "L") Then
                'Load
                ds.Rows(0).Item("Product_BUYER_no") = ""
                ds.Rows(0).Item("Product_BUYER_name") = ""
                ds.Rows(0).Item("Product_SELLER_no") = ""
                ds.Rows(0).Item("Product_SELLER_name") = ""
                ds.Rows(0).Item("LoadDestination") = ""
                ds.Rows(0).Item("DischDestination") = ""
                ds.Rows(0).Item("DestinationShow") = ""
                ds.Rows(0).Item("Owner_Product_no") = ""
                ds.Rows(0).Item("Owner_Product_name") = ""
            Else
                'Bulk
                'Load
                ds.Rows(0).Item("Product_BUYER_no") = ""
                ds.Rows(0).Item("Product_BUYER_name") = ""
                ds.Rows(0).Item("Product_SELLER_no") = ""
                ds.Rows(0).Item("Product_SELLER_name") = ""
                ds.Rows(0).Item("LoadDestination") = ""
                ds.Rows(0).Item("DischDestination") = ""
                ds.Rows(0).Item("DestinationShow") = ""

                ds.Rows(0).Item("Owner_Product_no") = ""
                ds.Rows(0).Item("Owner_Product_name") = ""

            End If

            ds.AcceptChanges()
        ElseIf (ViewState("index") & "" <> "") Then
            Dim index As Integer = CInt(ViewState("index"))
            ds.Rows(index).Item("Product_no") = Product_no
            ds.Rows(index).Item("Product_name") = txtProductName.Text
            ds.Rows(index).Item("Product_Amount") = txtProduct.Text
            ds.Rows(index).Item("Product_Event") = ddlProduct.SelectedValue
            ds.Rows(index).Item("Product_Description") = txtDescription.Text
            ds.Rows(index).Item("Product_Unit") = ddlUnit.SelectedValue
            ds.Rows(index).Item("Group_no") = Group_no
            ds.Rows(index).Item("Group_name") = Group_name
            If (ViewState("Port") = "L") Then
                'Load
                ds.Rows(index).Item("Product_BUYER_no") = ""
                ds.Rows(index).Item("Product_BUYER_name") = ""
                ds.Rows(index).Item("Product_SELLER_no") = ""
                ds.Rows(index).Item("Product_SELLER_name") = ""
                ds.Rows(index).Item("LoadDestination") = ""
                ds.Rows(index).Item("DischDestination") = ""
                ds.Rows(index).Item("DestinationShow") = ""
                ds.Rows(index).Item("Owner_Product_no") = ""
                ds.Rows(index).Item("Owner_Product_name") = ""
            Else
                'Bulk
                'Load
                ds.Rows(index).Item("Product_BUYER_no") = ""
                ds.Rows(index).Item("Product_BUYER_name") = ""
                ds.Rows(index).Item("Product_SELLER_no") = ""
                ds.Rows(index).Item("Product_SELLER_name") = ""
                ds.Rows(index).Item("LoadDestination") = ""
                ds.Rows(index).Item("DischDestination") = ""
                ds.Rows(index).Item("DestinationShow") = ""
                ds.Rows(index).Item("Owner_Product_no") = ""
                ds.Rows(index).Item("Owner_Product_name") = ""
            End If

            ds.AcceptChanges()
        Else
            Dim dr As DataRow = ds.NewRow()
            dr.Item("Product_no") = Product_no
            dr.Item("Product_name") = txtProductName.Text
            dr.Item("Product_Amount") = txtProduct.Text
            dr.Item("Product_Event") = ddlProduct.SelectedValue
            dr.Item("Product_Description") = txtDescription.Text
            dr.Item("Product_Unit") = ddlUnit.SelectedValue
            dr.Item("Group_no") = Group_no
            dr.Item("Group_name") = Group_name
            If (ViewState("Port") = "L") Then
                dr.Item("Product_BUYER_no") = ""
                dr.Item("Product_BUYER_name") = ""
                dr.Item("Product_SELLER_no") = ""
                dr.Item("Product_SELLER_name") = ""
                dr.Item("LoadDestination") = ""
                dr.Item("DischDestination") = ""
                dr.Item("DestinationShow") = ""
                dr.Item("Owner_Product_no") = ""
                dr.Item("Owner_Product_name") = ""
            Else
                'Bulk
                dr.Item("Product_BUYER_no") = ""
                dr.Item("Product_BUYER_name") = ""
                dr.Item("Product_SELLER_no") = ""
                dr.Item("Product_SELLER_name") = ""
                dr.Item("LoadDestination") = ""
                dr.Item("DischDestination") = ""
                dr.Item("DestinationShow") = ""

                dr.Item("Owner_Product_no") = ""
                dr.Item("Owner_Product_name") = ""
            End If

            ds.Rows.Add(dr)
            ds.AcceptChanges()
            End If
            ddlProduct.ClearSelection()
            txtProduct.Text = String.Empty
            txtProductName.Text = String.Empty
            txtDescription.Text = String.Empty
            ddlUnit.ClearSelection()

            Session("Product_popup") = ds
            Return True
    End Function

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class